package com.puny.mvc.models
{
	import com.puny.mvc.MvcEvent;
	
	import flash.events.Event;
	
	import flexunit.framework.TestCase;

	public class ModelTest extends TestCase
	{
		public function ModelTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testCreateInstance():void {
			//You can create an instance but it's useless.
			var test:EntityModels = new EntityModels();
			assertNotNull(test);
		}
		
		public function testCreateNewModel():void {
			//happy model instance will be created.
			//birthMonth and birthDay fields are added and set.
			var model:Model = EntityModels.getInstance("happy");
			model.birthMonth = 12;
			model.birthday = 25;
			assertEquals(12,model.birthMonth);
			assertEquals(25,model.birthday);
			EntityModels.dump();
		}
		
		public function testUseExistedModel():void {
			var dummy:Model = EntityModels.getInstance("holiday");
			dummy.birthMonth = 12;
			dummy.birthday = 25;
			
			//holiday model should exist.
			//It's already have values of birthMonth and birthDay.
			var model:Model = EntityModels.getInstance("holiday");
			assertEquals(12,model.birthMonth);
			assertEquals(25,model.birthday);

		}
				
		public function testUseNonExistVariable():void {
			var model:Model = EntityModels.getInstance("TestForNonExists");
			//Try to get a value from non exist variable.
			try{
				assertUndefined(model.test);
			} catch(e:Error) {
				assertEquals("Undefined property:test",e.message);
			}
		}
		
		public function testEventHandler():void {
			var model:Model = EntityModels.getInstance("TestForSingleEvent");
			model.addEventListener(MvcEvent.UPDATED, addAsync(updatedHandler, 4000));
			model.action = "Fired";
		}
		
		private function updatedHandler(event:Event):void {
			assertEquals("Fired", event.currentTarget.action);	
		}
		
		// The event handler was expected to be called 5 times but it is called once.
		public function testMultipleEventHandler():void {
			// an event handler should be called once when multiple variables are updated.
			var model:Model = EntityModels.getInstance("TestForMultipleEvents");
			model.addEventListener(MvcEvent.UPDATED, addAsync(multipleUpdatedHandler1, 4000));
			model.test1 = 1;
			model.test2 = 1;
			model.test3 = 1;
			model.test4 = 10;
			model.test5 = 1;

			// an event handler should not be called by the below
			var dummy:Model = EntityModels.getInstance("TestForMultipleEvents2");
			dummy.test1 = 1;
		}
		
		// This event handler was expected to be call 5 times but it is called once.
		private var multipleCounter1:uint = 0;
		private function multipleUpdatedHandler1(event:Event):void {
			multipleCounter1++;
			assertFalse(multipleCounter1 > 1);
//			if (multipleCounter1 > 1) {
//				fail("It shouldn't be called more than once.");
//			}
		}

		// this is the exact way to restrain that the multiple events happen.
		public function testMultipleEventHandler2():void {
			// an event handler should be called once when multiple variables are updated.
			var model:Model = EntityModels.getInstance("TestForMultipleEvents3");
			model.addEventListener(MvcEvent.UPDATED, addAsync(multipleUpdatedHandler2, 4000));
			model.beginUpdate();
			model.test1 = 1;
			model.test2 = 1;
			model.test3 = 1;
			model.test4 = 10;
			model.test5 = 1;
			model.commit();

			// an event handler should not be called by the below
			// because this is a different instance from the TestForMultipleEvent3.
			var dummy:Model = EntityModels.getInstance("TestForMultipleEvents4");
			dummy.test1 = 1;
		}
		
		private var multipleCounter2:uint = 0;
		private function multipleUpdatedHandler2(event:Event):void {
			multipleCounter2++;
			assertFalse(multipleCounter2 > 1);
//			if (multipleCounter2 > 1) {
//				fail("It shouldn't be called more than once.");
//			}
		}

	}
}